import { TILE } from "../../enum";
import { LoopFunc, MAP } from "../../Types";

/**  渲染有效墙壁，比如说内部的墙壁是不需要物理的*/
export const renderUsefulWall =
    (
        useful: (map: MAP, x: number, y: number) => void,
        useless?: (map: MAP, x: number, y: number) => void
    ): LoopFunc =>
    (map) => {
        let usefulArr: number[][] = [];
        let uselessArr: number[][] = [];

        map.$forEach((value: TILE, x: number, y: number) => {
            if (value === TILE.Wall) {
                let isVoidNeighbor = [
                    [x - 1, y],
                    [x + 1, y],
                    [x, y - 1],
                    [x, y + 1],
                ].some(([x, y]) => {
                    return map.get(x, y) !== TILE.Wall;
                });

                isVoidNeighbor
                    ? uselessArr.push([x, y])
                    : usefulArr.push([x, y]);
            }
        });

        usefulArr.forEach(([x, y]) => useful(map, x, y));
        if (useless) uselessArr.forEach(([x, y]) => useless(map, x, y));
        return map;
    };
